package com.hzncc.flowable_diboot.contract.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.hzncc.flowable_diboot.contract.entity.Konp;
import com.hzncc.flowable_diboot.contract.service.KonpService;
import com.hzncc.flowable_diboot.mapper.KonpMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @author 30401
 * @description 针对表【KONP(物料产品目录价中间表)】的数据库操作Service实现
 * @createDate 2024-08-19 11:40:31
 */
@DS("sap")
@Service
public class KonpServiceImpl extends MppServiceImpl<KonpMapper, Konp>
        implements KonpService {

    private final KonpMapper konpMapper;

    public KonpServiceImpl(KonpMapper konpMapper) {
        this.konpMapper = konpMapper;
    }

    /**
     * 根据物料编码获取物料信息
     *
     * @param matnrs
     * @return
     * @author 陈广煜
     * @date 2024-08-19
     */
    public List<Konp> getSapMrlByCode(List<String> matnrs) {
        QueryWrapper<Konp> qw = new QueryWrapper<>();
        qw.lambda().in(Konp::getMatnr, matnrs);
        return list(qw);
    }

    /**
     * 根据物料编码获取物料信息
     *
     * @param kunnr     sap客户编码
     * @param vkorg     销售组织
     * @param matnrList 物料编码
     * @return
     */
    public List<Konp> getSapMrl(String kunnr, String vkorg, List<String> matnrList) {
        QueryWrapper<Konp> qw = new QueryWrapper<>();
        String now = LocalDateTimeUtil.format(LocalDate.now(), "yyyyMMdd");
        qw.lambda().select(Konp::getKbetr, Konp::getMatnr, Konp::getMaktx).in(Konp::getMatnr, matnrList)
                //客户
                .eq(Konp::getKunnr, kunnr)
                // 销售组织
                .eq(Konp::getVkorg, vkorg)
                // 有效期
                .lt(Konp::getDatab, now).gt(Konp::getDatbi, now).eq(Konp::getLoevm, "X");
        return list(qw);
    }

    /**
     * 根据物料编码获取物料信息
     *
     * @param vkorg     销售组织
     * @param matnrList 物料编码
     * @return
     */
    public List<Konp> getSapMrl(String vkorg, List<String> matnrList) {
        QueryWrapper<Konp> qw = new QueryWrapper<>();
        String now = LocalDateTimeUtil.format(LocalDate.now(), "yyyyMMdd");
        qw.lambda().select(Konp::getKbetr, Konp::getMatnr, Konp::getMaktx).in(Konp::getMatnr, matnrList)
                // 销售组织
                .eq(Konp::getVkorg, vkorg)
                // 有效期
                .lt(Konp::getDatab, now).gt(Konp::getDatbi, now);
        return list(qw);
    }

    @Override
    public List<Konp> getSapMrlByCode(String kunnr, String vkorg, String matnr) {
        QueryWrapper<Konp> qw = new QueryWrapper<>();
        String now = LocalDateTimeUtil.format(LocalDate.now(), "yyyyMMdd");
        qw.lambda().select(Konp::getKbetr, Konp::getMatnr, Konp::getMaktx).like(Konp::getMatnr, matnr)
                //客户
                .eq(Konp::getKunnr, kunnr)
                // 销售组织
                .eq(Konp::getVkorg, vkorg)
                // 有效期
                .lt(Konp::getDatab, now).gt(Konp::getDatbi, now).eq(Konp::getLoevm, "X");
        List<Konp> list = list(qw);
        if (list == null || list.isEmpty()) {
            qw.clear();
            qw.lambda().select(Konp::getKbetr, Konp::getMatnr, Konp::getMaktx).like(Konp::getMatnr, matnr)
                    // 销售组织
                    .eq(Konp::getVkorg, vkorg)
                    // 有效期
                    .lt(Konp::getDatab, now).gt(Konp::getDatbi, now);
            return list(qw);
        } else {
            return list;
        }
    }

    @Override
    public Page<Konp> getSapMrlPage(String kunnr, String vkorg, String matnr, Page page) {
        String now = LocalDateTimeUtil.format(LocalDate.now(), "yyyyMMdd");
        List<Konp> mrl = konpMapper.getMrl("%" + matnr + "%", vkorg, kunnr, now, (int) page.getCurrent() - 1, (int) page.getSize());
        QueryWrapper<Konp> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Konp::getVkorg, vkorg)
                .eq(Konp::getKunnr,kunnr)
                .lt(Konp::getDatab, now)
                .gt(Konp::getDatbi, now)
                .like(Konp::getMatnr, matnr);
        if (mrl.isEmpty()) {
            mrl = konpMapper.getMrlWithoutKunnr("%" + matnr + "%", vkorg, now, (int) page.getCurrent() - 1, (int) page.getSize());
            queryWrapper.clear();
            queryWrapper.lambda()
                    .eq(Konp::getVkorg, vkorg)
                    .lt(Konp::getDatab, now)
                    .gt(Konp::getDatbi, now)
                    .like(Konp::getMatnr, matnr);
        }
        Long count = konpMapper.selectCount(queryWrapper);
        page.setRecords(mrl);
        page.setTotal(count);
        return page;
    }
}




